import streamlit as st

# 设置页面配置
st.set_page_config(
    layout="wide",
    page_title="机器猫智能客服系统",
    page_icon="favicon.ico",
    initial_sidebar_state="expanded",
    menu_items={
        "Get Help": None,  # 隐藏帮助菜单
        "Report a bug": None,  # 隐藏报告问题菜单
        "About": None,  # 隐藏关于菜单
    }
)

import os
from auth import is_logged_in, logout, get_current_user
from ai_doraemon import render_layout
# from ai_left_sidebar import render_secondary_navigation
# from ai_right_main import render_main
from km import render_km  # 导入 km.py 中的 render_km 函数
from setting import render_setting  # 导入 setting.py 中的 render_setting 函数

# 获取当前路径
curr_path = os.getcwd()
image_path = os.path.join(curr_path, "assets")

# 加载图标
def load_icon(icon_name):
    """加载图标"""
    return os.path.join(image_path, f"{icon_name}.png")

# 尝试直接注入favicon链接，以确保其被正确加载
st.markdown(
    """
    <link rel="icon" href="./favicon.ico" type="image/x-icon">
    """,
    unsafe_allow_html=True,
)

# 自定义左侧导航栏
def render_custom_sidebar():
    """渲染左侧导航栏"""
    with st.sidebar:
        # Logo 和文字
        col1, col2 = st.columns([1, 3])
        with col1:
            st.image(load_icon("logo"), width=46)  # Logo 大小设置为 46px
        with col2:
            st.markdown(
                """
                <style>
                    .logo-text {
                        font-size: 20px;
                        font-weight: bold;
                        margin-top: 10px;
                    }
                </style>
                <div class="logo-text">Doraemon-Bot</div>
                """,
                unsafe_allow_html=True,
            )
        st.markdown("---")
        # 页面导航按钮
        if st.button("🏠 机器猫助手", key="main_page_button"):
            st.session_state.current_page = "main"
        if st.button("📚 知识管理", key="km_page_button"):
            st.session_state.current_page = "km"
        if st.button("⚙️ 系统设置", key="setting_page_button"):
            st.session_state.current_page = "setting"

        # 渲染用户信息
        render_user_info()
        # 版权信息
        st.markdown("---")
        st.markdown("©2024-2025 Doraemon-Bot 光环AIE-51-1G保留所有权利。")


def render_user_info():
    """渲染用户信息和退出按钮"""
    user = get_current_user()
    st.markdown("---")
    st.markdown(f"**欢迎, {user['username']} [{user['role']}]**")
    if st.button("🚀 退出登录", help="退出登录"):
        logout()
        st.rerun()

# 隐藏多页面导航栏的自定义 CSS
hide_pages_nav = """
<style>
    /* 隐藏多页面导航栏 */
    .stApp [data-testid="stSidebarNav"] {
        display: none;
    }
</style>
"""
# 注入自定义 CSS
st.markdown(hide_pages_nav, unsafe_allow_html=True)

# 初始化 st.session_state
if "user" not in st.session_state:
    st.session_state.user = None

if "current_page" not in st.session_state:
    st.session_state.current_page = "main"  # 默认显示 main.py 的内容

# 获取当前用户
user = get_current_user()

# 根据登录状态显示不同内容
if not is_logged_in():
    st.switch_page("pages/1_login.py")  # 跳转到登录页面
else:
    # 渲染左侧导航栏
    render_custom_sidebar()

    # 根据当前选中的页面渲染右侧内容
    if st.session_state.current_page == "main":
        render_layout()
        # col1, col2 = st.columns([1, 3])  # 左侧占 1 份，右侧占 3 份
        # with col1:
        #     # 渲染二级导航
        #     st.write("二级导航内容")
        #     selected_conversation = render_secondary_navigation()  # 渲染二级导航
        # with col2:
        #     # 渲染主内容
        #     if not selected_conversation:
        #         st.session_state.is_new_conversation = True  # 标记为新会话模式
        #         selected_conversation = "new_conversation"  # 设置一个默认值
        #     render_main(selected_conversation)  # 渲染 main.py 的内容
    elif st.session_state.current_page == "km":
        render_km()  # 渲染 km.py 的内容
    elif st.session_state.current_page == "setting":
        render_setting()  # 渲染 setting.py 的内容